#include "LogManager.h"
#include "TextEditor.h"
#include "ConfigManager.h"

#include <QFileInfo>
#include <QDir>

LogManager::LogManager()
{
}

void LogManager::Initialize()
{
	QDir logDir(CONFIG_MANAGER->GetLogPath());
	logDir.setFilter(QDir::Files);
	QFileInfoList fileList = logDir.entryInfoList();
	if (!fileList.isEmpty())
	{
		QList<int> loc;
		QFile file;
		QByteArray rawData;
		for (int i = 0; i < fileList.count(); i++)
		{
			file.setFileName(logDir.path() + '/' + fileList.at(i).fileName());
			if (file.open(QIODevice::ReadOnly))
			{
				rawData = file.readAll();
				if (!rawData.isEmpty())
				{
					for (int j = 0; j < rawData.length(); j++)
					{
						if (*(rawData.data() + j) == '#')
							loc << j;
					}

					int head, tail;
					for (int j = 0; j < loc.count(); j++)
					{
						head = loc.at(j);
						if (head == loc.last())
							tail = rawData.length();
						else
							tail = loc.at(j+1);
						QByteArray logStr = rawData.mid(head, tail-head);

						if (logStr.contains("#Send"))
							_txList.append(TEXT_EDITOR->LogReadTextProcessing(logStr));
						else if (logStr.contains("#Received"))
							_rxList.append(TEXT_EDITOR->LogReadTextProcessing(logStr));
					}
					loc.clear();
				}
				file.close();
			}
			else
				qDebug() << "LogManager::Initialize() : "<< file.fileName() << "open failed ";
		}
	}

	int excnt;
	excnt = _txList.count() - CONFIG_MANAGER->GetLogCapacity();
	if (excnt > 0)
		DeleteOldLogs(true, excnt);
	excnt = _rxList.count() - CONFIG_MANAGER->GetLogCapacity();
	if (excnt > 0)
		DeleteOldLogs(false, excnt);

}

bool LogManager::AddLog(QString msg, bool isTx)
{
	int excnt;
	if (isTx)
	{
		_txList.append(msg);
		excnt = _txList.count() - CONFIG_MANAGER->GetLogCapacity();
	}
	else
	{
		_rxList.append(msg);
		excnt = _rxList.count() - CONFIG_MANAGER->GetLogCapacity();
	}
	if (excnt > 0)
		DeleteOldLogs(isTx, excnt);

	bool add = false;
	QFile wFile;
	wFile.setFileName(QString(CONFIG_MANAGER->GetLogPath() + TEXT_EDITOR->GetCurrentTimeforLog()));
	if (wFile.exists())
		wFile.open(QIODevice::Append);
	else
		wFile.open(QIODevice::WriteOnly);
	if (wFile.isOpen())
	{
		add = (wFile.write(TEXT_EDITOR->LogWriteTextProcessing(msg, isTx).toAscii().data()) != -1);
		wFile.close();
	}
	else
		add = false;
	return add;
}

QString LogManager::GetLog(bool isTx, int idx) const
{
	QString log = "";
	if (idx > 0)
	{
		if (isTx && _txList.count())
			log = _txList.at(idx-1)	;
		else if (!isTx && _rxList.count())
			log = _rxList.at(idx-1);
	}
	return log;
}

void LogManager::DeleteAllLog()
{
	_txList.clear();
	_rxList.clear();
}

void LogManager::DeleteLog(bool isTx)
{
	if (isTx)
		_txList.clear();
	else
		_rxList.clear();
}

void LogManager::DeleteLog(bool isTx, int idx)
{
	if (idx > 0)
	{
		if (isTx)
			_txList.removeAt(idx-1);
		else
			_rxList.removeAt(idx-1);
	}
}

int LogManager::GetLogCount(bool isTx) const
{
	if (isTx)
		return _txList.count();
	else
		return _rxList.count();
}

void LogManager::DeleteOldLogs(bool isTx, int delCnt)
{
	if (isTx)
	{
		for (int i = 0; i < delCnt; i++)
			_txList.removeFirst();
	}
	else
	{
		for (int i = 0; i < delCnt; i++)
			_rxList.removeFirst();
	}
}
